using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VVLZEIT
    {
        public DataRow Convert2VLZEIT(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryVLZEIT(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["VLZMANDANT"] = " ".PadRight(2 ); 
         dwReturn["VLZLO"] = " ".PadRight(2 ); 
         dwReturn["VLZPROGRP"] = " ".PadRight(3 ); 
         dwReturn["VLZVERSART"] = " ".PadRight(3 ); 
         dwReturn["VLZSPED"] = " ".PadRight(3 ); 
         dwReturn["VLZPOSTYP"] = " ".PadRight(3 ); 
         dwReturn["VLZLAND"] = " ".PadRight(3 ); 
         dwReturn["VLZUHRBIS"] = " ".PadRight(0 ); 
         dwReturn["VLZVDZEIT"] = " ".PadRight(0 ); 
         dwReturn["VLZVKZEIT"] = " ".PadRight(0 ); 
         dwReturn["VLZZUZEIT"] = " ".PadRight(0 ); 
         dwReturn["VLZFILLER"] = " ".PadRight(12 ); 
         dwReturn["VLZAENZL"] = " ".PadRight(0 ); 
         dwReturn["VLZAENB"] = " ".PadRight(8 ); 
         dwReturn["VLZAEND"] = " ".PadRight(0 ); 
         dwReturn["VLZNEUB"] = " ".PadRight(8 ); 
         dwReturn["VLZNEUD"] = " ".PadRight(0 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapVLZEIT(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveVLZEIT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryVLZEIT(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapVLZEIT(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertVLZEIT(dwLocal, ITXDBVERSION);
            }
            UpdateVLZEIT(dwLocal);
        }
        public void InsertVLZEIT(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO VLZEIT(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryVLZEIT(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM VLZEIT WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM VLZEIT  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "VLZEIT";
            Return = Result;
            return Return;
        }
        public void DeleteVLZEIT(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM VLZEIT   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateVLZEIT(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE VLZEIT SET "
                    +"VLZMANDANT= :VLZMANDANT,"
                    +"VLZLO= :VLZLO,"
                    +"VLZPROGRP= :VLZPROGRP,"
                    +"VLZVERSART= :VLZVERSART,"
                    +"VLZSPED= :VLZSPED,"
                    +"VLZPOSTYP= :VLZPOSTYP,"
                    +"VLZLAND= :VLZLAND,"
                    +"VLZUHRBIS= :VLZUHRBIS,"
                    +"VLZVDZEIT= :VLZVDZEIT,"
                    +"VLZVKZEIT= :VLZVKZEIT,"
                    +"VLZZUZEIT= :VLZZUZEIT,"
                    +"VLZFILLER= :VLZFILLER,"
                    +"VLZAENZL= :VLZAENZL,"
                    +"VLZAENB= :VLZAENB,"
                    +"VLZAEND= :VLZAEND,"
                    +"VLZNEUB= :VLZNEUB,"
                    +"VLZNEUD= :VLZNEUD,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("VLZMANDANT", OracleType.NVarChar).Value = dwData["VLZMANDANT"].ToString(); 
          or1.Parameters.Add("VLZLO", OracleType.NVarChar).Value = dwData["VLZLO"].ToString(); 
          or1.Parameters.Add("VLZPROGRP", OracleType.NVarChar).Value = dwData["VLZPROGRP"].ToString(); 
          or1.Parameters.Add("VLZVERSART", OracleType.NVarChar).Value = dwData["VLZVERSART"].ToString(); 
          or1.Parameters.Add("VLZSPED", OracleType.NVarChar).Value = dwData["VLZSPED"].ToString(); 
          or1.Parameters.Add("VLZPOSTYP", OracleType.NVarChar).Value = dwData["VLZPOSTYP"].ToString(); 
          or1.Parameters.Add("VLZLAND", OracleType.NVarChar).Value = dwData["VLZLAND"].ToString(); 
          or1.Parameters.Add("VLZUHRBIS", OracleType.NVarChar).Value = dwData["VLZUHRBIS"].ToString(); 
          or1.Parameters.Add("VLZVDZEIT", OracleType.NVarChar).Value = dwData["VLZVDZEIT"].ToString(); 
          or1.Parameters.Add("VLZVKZEIT", OracleType.NVarChar).Value = dwData["VLZVKZEIT"].ToString(); 
          or1.Parameters.Add("VLZZUZEIT", OracleType.NVarChar).Value = dwData["VLZZUZEIT"].ToString(); 
          or1.Parameters.Add("VLZFILLER", OracleType.NVarChar).Value = dwData["VLZFILLER"].ToString(); 
          or1.Parameters.Add("VLZAENZL", OracleType.NVarChar).Value = dwData["VLZAENZL"].ToString(); 
          or1.Parameters.Add("VLZAENB", OracleType.NVarChar).Value = dwData["VLZAENB"].ToString(); 
          or1.Parameters.Add("VLZAEND", OracleType.NVarChar).Value = dwData["VLZAEND"].ToString(); 
          or1.Parameters.Add("VLZNEUB", OracleType.NVarChar).Value = dwData["VLZNEUB"].ToString(); 
          or1.Parameters.Add("VLZNEUD", OracleType.NVarChar).Value = dwData["VLZNEUD"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
